package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;

import logic.Message;
import java.awt.Color;
import javax.swing.UIManager;

public class CreateMember extends JPanel {
//cfhbdfgxcjhncfvbnhb 
	ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
	ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
	
	ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
	ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
	
	private Client_GUI parent;
	private JTextField nameTextField;
	private JTextField passTextField;
	private JTextField pass2TextField;
	private JTextField uidTextField;
	private JTextField dobTextField;
	private JTextField phoneTextField;
	private JTextField addressTextField;
	private JTextField emailTextField;
	
	private JLabel nameLBL;
	
	private JLabel uidLBL;
	private JLabel dobLBL;
	private JLabel passLBL;
	private JLabel phoneLBL;
	private JLabel emailLBL;
	private JLabel addressLBL;
	
	public CreateMember(final Client_GUI parent) {
		this.parent = parent;
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.setSize(parent.MAIN_WIDTH, parent.MAIN_HEIGHT);
		setLayout(null);
		
		JLabel lblCreateAMember = new JLabel("Create A Member");
		lblCreateAMember.setForeground(Color.RED);
		lblCreateAMember.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblCreateAMember.setBounds(268, 22, 260, 44);
		add(lblCreateAMember);
		
		JLabel lblUserid = new JLabel("UserID");
		lblUserid.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblUserid.setBounds(147, 194, 141, 14);
		add(lblUserid);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPassword.setBounds(147, 122, 141, 14);
		add(lblPassword);
		
		JLabel lblStudentName = new JLabel("Student Name:");
		lblStudentName.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblStudentName.setBounds(147, 97, 141, 14);
		add(lblStudentName);
		
		JLabel lblDateOfBirth = new JLabel("Date Of Birth:");
		lblDateOfBirth.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblDateOfBirth.setBounds(147, 219, 141, 14);
		add(lblDateOfBirth);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPhone.setBounds(147, 244, 141, 14);
		add(lblPhone);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblAddress.setBounds(147, 269, 141, 14);
		add(lblAddress);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblEmail.setBounds(147, 294, 141, 14);
		add(lblEmail);
		
		nameTextField = new JTextField();
		nameTextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		nameTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});

		nameTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		nameTextField.setBounds(298, 96, 198, 20);
		add(nameTextField);
		nameTextField.setColumns(10);
		
		passTextField = new JTextField();
		passTextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		passTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});
		passTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		passTextField.setBounds(298, 124, 198, 20);
		add(passTextField);
		passTextField.setColumns(10);
		
		uidTextField = new JTextField();
		uidTextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		uidTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});
		uidTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		uidTextField.setBounds(298, 196, 198, 20);
		add(uidTextField);
		uidTextField.setColumns(10);
		
		dobTextField = new JTextField();
		dobTextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		dobTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});
		dobTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		dobTextField.setBounds(298, 221, 198, 20);
		add(dobTextField);
		dobTextField.setColumns(10);
		
		phoneTextField = new JTextField();
		phoneTextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		phoneTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});
		phoneTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		phoneTextField.setBounds(298, 246, 198, 20);
		add(phoneTextField);
		phoneTextField.setColumns(10);
		
		addressTextField = new JTextField();
		addressTextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		addressTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});
		addressTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		addressTextField.setBounds(298, 271, 198, 20);
		add(addressTextField);
		addressTextField.setColumns(10);
		
		emailTextField = new JTextField();
		emailTextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		emailTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});
		emailTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		emailTextField.setBounds(298, 296, 198, 20);
		add(emailTextField);
		emailTextField.setColumns(10);
		
		JLabel lblReenterPassword = new JLabel("Re-Enter Password:");
		lblReenterPassword.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblReenterPassword.setBounds(147, 147, 141, 14);
		add(lblReenterPassword);
		
		pass2TextField = new JTextField();
		pass2TextField.setFont(new Font("Tahoma", Font.BOLD, 13));
		pass2TextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ValidateData(false);
			}
		});
		pass2TextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				parent.safeToClear = false;
			}
		});
		pass2TextField.setBounds(298, 146, 198, 20);
		add(pass2TextField);
		pass2TextField.setColumns(10);
		
		JButton btnCreateMember = new JButton("Create Member");
		btnCreateMember.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnCreateMember.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				ValidateData(true);
				

			}
		});
		btnCreateMember.setBounds(176, 423, 156, 35);
		add(btnCreateMember);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Home home = new Home(parent,parent.client.publicMessages);

				VerifyDialog verify = new VerifyDialog(parent, home,false);

				
			}
		});
		btnCancel.setBounds(477, 423, 156, 35);
		add(btnCancel);
		
		nameLBL = new JLabel("");
		nameLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		nameLBL.setBounds(506, 97, 22, 22);
		add(nameLBL);
		
		uidLBL = new JLabel("");
		uidLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		uidLBL.setBounds(506, 194, 22, 22);
		add(uidLBL);
		
		JLabel lblexampleYyyymmdd = new JLabel("[example: 2000-02-20]");
		lblexampleYyyymmdd.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblexampleYyyymmdd.setBounds(549, 219, 162, 14);
		add(lblexampleYyyymmdd);
		
		dobLBL = new JLabel("");
		dobLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		dobLBL.setBounds(506, 219, 22, 22);
		add(dobLBL);
		
		passLBL = new JLabel("");
		passLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		passLBL.setBounds(503, 144, 22, 22);
		add(passLBL);
		
		phoneLBL = new JLabel("");
		phoneLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		phoneLBL.setBounds(506, 244, 22, 22);
		add(phoneLBL);
		
		emailLBL = new JLabel("");
		emailLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		emailLBL.setBounds(503, 294, 22, 22);
		add(emailLBL);
		
		addressLBL = new JLabel("");
		addressLBL.setFont(new Font("Tahoma", Font.BOLD, 13));
		addressLBL.setBounds(506, 269, 22, 22);
		add(addressLBL);
	}
	
	private void ValidateData(boolean fromOK){
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		
		//Validate UID
		temp = uidTextField.getText();
		if (!temp.equals(""))
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-' || temp.length()!=9) {
				uidLBL.setIcon(BAD);
				valid = false;
			}
			else uidLBL.setIcon(OK);

			
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){
			uidLBL.setIcon(BAD);
			valid = false;
		}
		else uidLBL.setIcon(null);
		
		
		//validate DOB
		temp = dobTextField.getText();
		if(!temp.equals(""))
		try
		{
			if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
				dobLBL.setIcon(OK);
			else {
				dobLBL.setIcon(BAD);
				valid = false;
			}
			
			int year = Integer.parseInt(temp.substring(0, 4));
			int month = Integer.parseInt(temp.substring(5, 7));
			int day = Integer.parseInt(temp.substring(8, 10));

			Calendar validator = Calendar.getInstance();
			validator.set(year, month-1, day);
			validator.setLenient(false);
			validator.getTime();
			
		}catch(NumberFormatException e){
			dobLBL.setIcon(BAD);
			valid = false;
		}catch(Exception e){
			dobLBL.setIcon(BAD);
			valid = false;
		}
		else dobLBL.setIcon(null);
		
		//validate Password confirmation
		if (!passTextField.getText().equals("")){
			if (passTextField.getText().equals(pass2TextField.getText())) passLBL.setIcon(OK);
			else {
				passLBL.setIcon(BAD);
				valid = false;
			}
		}
		else passLBL.setIcon(null);
		
		
		//validate phone
		if (!phoneTextField.getText().equals("")){
			String phone = phoneTextField.getText();
			phone = phone.replace("-", "");
			phone = phone.replace(" ", "");
			
			try{
				Integer.parseInt(phone);
				phoneLBL.setIcon(OK);
			}catch(NumberFormatException e){
				phoneLBL.setIcon(BAD);
				valid = false;
			}
			if(phoneTextField.getText().length()!=9 && phoneTextField.getText().length()!=10
					&& phoneTextField.getText().length()!=11)
	{
		phoneLBL.setIcon(BAD);
		valid = false;
	}
		}
		else phoneLBL.setIcon(null);
		
		//validate Email
		if (!emailTextField.getText().equals("")){
			int schtroodelLocation = emailTextField.getText().indexOf("@");
			
			if (emailTextField.getText().indexOf("@", schtroodelLocation) == -1){//another @ exists
				emailLBL.setIcon(BAD);
				valid = false;
			}else if (emailTextField.getText().indexOf(".", schtroodelLocation) == -1){
				emailLBL.setIcon(BAD);
				valid = false;
			}else if (schtroodelLocation == 0){
				emailLBL.setIcon(BAD);
				valid = false;
			}else emailLBL.setIcon(OK);
		}
		else emailLBL.setIcon(null);
		
		//validate name
		if (!nameTextField.getText().equals("")) nameLBL.setIcon(OK);
		else nameLBL.setIcon(null);
		
		//validate address
		if (!addressTextField.getText().equals("")) addressLBL.setIcon(OK);
		else addressLBL.setIcon(null);
		
		if (fromOK){
			if (emailTextField.getText().equals("")){
				emailLBL.setIcon(BAD);
				valid = false;
			}
			if (phoneTextField.getText().equals("")){
				phoneLBL.setIcon(BAD);
				valid = false;
			}
			if (addressTextField.getText().equals("")){
				addressLBL.setIcon(BAD);
				valid = false;
			}
			if (dobTextField.getText().equals("")){
				dobLBL.setIcon(BAD);
				valid = false;
			}
			if (uidTextField.getText().equals("")){
				uidLBL.setIcon(BAD);
				valid = false;
			}
			if (passTextField.getText().equals("") || pass2TextField.getText().equals("")){
				passLBL.setIcon(BAD);
				valid = false;
			}
			if (nameTextField.getText().equals("")){
				nameLBL.setIcon(BAD);
				valid = false;
			}
		}
		
		
		if (valid && fromOK){
			int action = Message.CREATE_MEMBER_REQUEST;
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
			
			index.add("UID");
			data.add(uidTextField.getText());
			
			index.add("PASSWORD");
			data.add(passTextField.getText());
			
			index.add("NAME");
			data.add(nameTextField.getText());
			
			index.add("DOB");
			data.add(dobTextField.getText());
			
			index.add("PHONE");
			data.add(phoneTextField.getText());
			
			index.add("MAIL");
			data.add(emailTextField.getText());
			
			index.add("ADDRESS");
			data.add(addressTextField.getText());
			
			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
				
				//TODO handle accept request on server side, and then sent result
				
			}else{//message send failed
				//TODO verify extream situation
			}
			
		}
	}
}
